package Componand.XmiReader;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Model.Actor;
import Model.Association;
import Model.Classe;
import Model.Normal;
import Model.Usecase;
import Model.diagram.ClassDiagram;
import Model.diagram.UsecasDiagram;
import Model.interfaces.Iassociation;
import Model.interfaces.Idiagrame;

public class XmiReaderClassFactory {
	private Map<String,Classe> ClasMap;
	private Association a;
	private Map<String,Iassociation> associationmap;
	private boolean isUsecaseDiagram=false;
	final Pattern patternid = Pattern.compile("xmi:type='uml:(.+?)'");
	final Pattern patternclassname = Pattern.compile("name='(.+?)'");
	final Pattern patternobjectid = Pattern.compile("xmi:id='(.+?)'");
	final Pattern patterntype = Pattern.compile("type='(.+?)'");
	final Pattern patternassid = Pattern.compile("association='(.+?)'");
	final Pattern patternogetassclasses = Pattern.compile("<ownedEnd(.+?)>");
	
	
	public XmiReaderClassFactory(){
		this.ClasMap=new LinkedHashMap<String,Classe>();
		this.associationmap=new  LinkedHashMap<String,Iassociation>();
	}
	
	public void addClas(String rule){
	Matcher type=patternid.matcher(rule);
	while(type.find()){
		
		if(type.group(1).toLowerCase().equals("actor")){
			isUsecaseDiagram=true;
			
			Actor actor=new Actor(name(rule));
			ClasMap.put(id(rule), actor);
			
		}
		if(type.group(1).toLowerCase().equals("usecase")){
		
			Usecase usecase=new Usecase(name(rule));
			ClasMap.put(id(rule), usecase);
			
		}
		if(type.group(1).toLowerCase().equals("class")){
			
			Normal normal=new Normal(name(rule));
			ClasMap.put(id(rule), normal);
		}
	
		
	}
	
	
	
		
	
	}
	
public void addAssociation(String line){
		Association association=new Association();
		
		associationmap.put(id(line), association);
		
		
	}

public void addAssosiationData(String Content){
	Matcher findelements= patternogetassclasses.matcher(Content);
	while(findelements.find()){
		Matcher ass=patternassid.matcher(findelements.group(1));
		while(ass.find()){
		 a=	(Association)associationmap.get(ass.group(1));
		
		
		
		Matcher getclass =patterntype.matcher(findelements.group(1));
			while(getclass.find()){
			
				
				
				a.addUpper(ClasMap.get(getclass.group(1)));
			}
		}
	}
}
	public String id(String rule){
		String id=null;
		Matcher idtype=patternobjectid.matcher(rule);
		idtype.find();
		id=idtype.group(1);
		return id;
	}
	public String name(String rull){
		String name=null;
		Matcher idtype=patternclassname.matcher(rull);
		idtype.find();
		name=idtype.group(1);
		
		return name;
	}
	public Idiagrame create(){
		Idiagrame temp=null;
		if(isUsecaseDiagram){
			temp=new UsecasDiagram();
			temp.addClass(ClasMap);
			temp.addAssociationlist(associationmap);
			
		
		}else{
			temp=new ClassDiagram();
			temp.addClass(ClasMap);
			temp.addAssociationlist(associationmap);
		}
		
		
		return temp;
	}
}


